package com.xquant.pricing.pricingConfig.service;


import cn.hutool.core.map.MapUtil;
import com.github.pagehelper.PageInfo;
import com.xquant.common.core.constant.DictConstant;
import com.xquant.pricing.pricingConfig.mapper.OptionContractMapper;
import com.xquant.pricing.pricingConfig.mapper.PricingConfigAuthorMapper;
import com.xquant.pricing.pricingConfig.mapper.TargetParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ershuai.zhao
 * *
 * @Date: 24/12/2019 11:42
 * *
 * @Description:
 */
@Transactional
public abstract class PricingConfigAuthorServiceAdapter implements  PricingConfigAuthorService {

    @Autowired
    private PricingConfigAuthorMapper pricingConfigAuthorMapper;

    @Autowired
    private OptionContractMapper optionContractMapper;

    @Autowired
    private TargetParamMapper targetParamMapper;


    @Override
    public void savePricingConfig(Map map) {
        pricingConfigAuthorMapper.savePricingConfig(map);
        pricingConfigAuthorMapper.updateUserPermCfg(map);
        pricingConfigAuthorMapper.updateDeptPermCfg(map);
    }

    @Override
    public void updatePricingConfig(Map map) {
        pricingConfigAuthorMapper.updatePricingConfig(map);
    }

    @Override
    public void savePricingConfigUserAuthor(Map map) {
        pricingConfigAuthorMapper.savePricingConfigUserAuthor(map);
    }

    @Override
    public void updatePricingConfigUserAuthor(Map map) {
        pricingConfigAuthorMapper.updatePricingConfigUserAuthor(map);
    }

    @Override
    public void deletePricingConfigUserAuthor(Map map) {
        pricingConfigAuthorMapper.deletePricingConfigUserAuthor(map);
        //删除用户和部门权限表数据
        pricingConfigAuthorMapper.removeUserPermCfg(map);
        pricingConfigAuthorMapper.removeDeptPermCfg(map);
        //删除读写权限表数据
        pricingConfigAuthorMapper.removeUserCfgByCfgId(map);
        pricingConfigAuthorMapper.removeDeptCfgByCfgId(map);

    }

    @Override
    public void deleteUserPriceCfg(Map map) {
        pricingConfigAuthorMapper.deleteSysUserPricCfg(map);
    }

    @Override
    public PageInfo<Map> queryPricingConfigUserAuthor(Map map) {
        List<Map> list = pricingConfigAuthorMapper.queryPricingConfigUserAuthor(map);
        return new PageInfo<Map>(list);
    }

    @Override
    public PageInfo<Map> queryGroupPricingConfigAuthorByPage(Map map) {
        List<Map> list = pricingConfigAuthorMapper.queryGroupPricingConfigAuthorByPage(map);
        return new PageInfo<Map>(list);
    }

    @Override
    public List<Map> queryPricingConfigUserDefaultAuthor(Map map) {
        List<Map> list = pricingConfigAuthorMapper.queryPricingConfigUserDefaultAuthor(map);
        return list;
    }


    @Override
    public void updateUserDefaultPricingParam(Map<String, Object> params) {
        pricingConfigAuthorMapper.deleteUserDefaultPricingParam(params);
        pricingConfigAuthorMapper.saveUserDefaultPricingParam(params);
    }

    @Override
    public PageInfo<Map> queryPricingConfigAuthorByPage(Map map) {
        List<Map> list = pricingConfigAuthorMapper.queryPricingConfigAuthorByPage(map);
        return new PageInfo<Map>(list);
    }

    @Override
    public List<Map> queryPricingConfigAuthor(Map map) {
        List<Map> list = pricingConfigAuthorMapper.queryPricingConfigAuthor(map);
        return list;
    }

    @Override
    public PageInfo<Map> queryPricingUserAuthorByPage(Map map) {
        List<Map> list = pricingConfigAuthorMapper.queryPricingUserAuthorByPage(map);
        return new PageInfo<Map>(list);
    }

    @Override
    public void executePricingConfigSyncAuthor(Map<String, Object> params) {
        deletePricingConfigSyncAuthor(params);
        savePricingConfigSyncAuthor(params);
    }

    @Override
    public void updateDefaultOtherAccRight(Map map) {
        pricingConfigAuthorMapper.updateDefaultOtherAccRight(map);
        List<Map> list = pricingConfigAuthorMapper.selectDeptCfgRight(map);
        for (Map dept : list) {
            String cfgId = (String) dept.get("cfg_id");
            if (cfgId == null || "".equals(cfgId)) {
                cfgId = (String) map.get("cfgId");
            } else {
                cfgId = cfgId.concat(","+map.get("cfgId"));
            }
            dept.put("cfg_id", cfgId);
            pricingConfigAuthorMapper.updateDeptPremcfgRight(dept);
        }
        List<String> deptIds = pricingConfigAuthorMapper.selectDefaultDeptPricCfg(map);
        if ( deptIds.size() ==  0 ) {
            map.put("deptIds", null);
        } else {
            map.put("deptIds", deptIds);
        }
        pricingConfigAuthorMapper.insertAllDeptRight(map);
    }

    @Override
    public void selectDefaultOtherPerm(Map map) {
        List<Map> list = pricingConfigAuthorMapper.selectDefaultOtherPerm(map);
        for (Map userPerm : list) {
            String cfgId = (String) userPerm.get("cfg_id");
            if (cfgId == null || "".equals(cfgId)) {
                cfgId = (String) map.get("cfgId");
            } else {
                cfgId = cfgId.concat(","+map.get("cfgId"));
            }
            userPerm.put("cfg_id", cfgId);
            //针对已有权限进行修改处理，设为已读
            pricingConfigAuthorMapper.updateUserPermCfgId(userPerm);
        }
        //新增除已有权限用户之外的数据，均为只读
        pricingConfigAuthorMapper.insertUserPermCfgId(map);
    }



    @Override
    /**
     * 更新定价配置内的浮动参数，并将所有用户(不更改原有权限，其余的赋为只读)和部门(写权限)都赋予权限
     * botao.yu
     */
    public void updateUserPermByFloatConfig(Map map) {
        List<Map<String, Object>> contractList =optionContractMapper.queryContractCfg(map);
        List<Map> targetList = targetParamMapper.queryTargetParamCfgByPage(map);
        List<String> disList=new ArrayList<>();
        contractList.stream().forEach(obj->{
            if(DictConstant.YES_NO.YES.equals(obj.get("DIS_TYPE"))){
                disList.add(MapUtil.getStr(obj,"DIS_VALUE"));
                //更新tircuve_user全员数据 不影响原有权限
                pricingConfigAuthorMapper.insertAllUserIrRight(MapUtil.getStr(obj,"DIS_VALUE"));
                //更新tircuve_dept全员数据 赋值为2:读写
                pricingConfigAuthorMapper.insertAllDeptIrRight(MapUtil.getStr(obj,"DIS_VALUE"));
            }
        });
        //更新原权限为无权限的用户为只读权限
        if(disList.size()>0) {
            pricingConfigAuthorMapper.updateAllUserIrRight(disList);
        }
        List<String> divList=new ArrayList<>();
        List<String> volList=new ArrayList<>();
        targetList.stream().forEach(obj->{
            if(DictConstant.YES_NO.YES.equals(obj.get("DIV_TYPE"))){
                divList.add(MapUtil.getStr(obj,"DIV_VALUE"));
                //更新tircuve_user全员数据 不影响原有权限
                pricingConfigAuthorMapper.insertAllUserDivRight(MapUtil.getStr(obj,"DIV_VALUE"));
                //更新tircuve_dept全员数据 赋值为2:读写
                pricingConfigAuthorMapper.insertAllDeptDivRight(MapUtil.getStr(obj,"DIV_VALUE"));
            }
            if(DictConstant.YES_NO.YES.equals(obj.get("VOL_TYPE"))){
                volList.add(MapUtil.getStr(obj,"VOL_VALUE"));
                //更新tircuve_user全员数据 不影响原有权限
                pricingConfigAuthorMapper.insertAllUserVlsRight(MapUtil.getStr(obj,"VOL_VALUE"));
                //更新tircuve_dept全员数据 赋值为2:读写
                pricingConfigAuthorMapper.insertAllDeptVlsRight(MapUtil.getStr(obj,"VOL_VALUE"));
            }
        });
        //更新原权限为无权限的用户为只读权限
        if(divList.size()>0) {
            pricingConfigAuthorMapper.updateAllUserDivRight(divList);
        }
        if(volList.size()>0) {
            pricingConfigAuthorMapper.updateAllUserVlsRight(volList);
        }
        //查询出所有的 userPerm
        List<Map> list = pricingConfigAuthorMapper.selectDefaultOtherPerm(new HashMap());
        for (Map userPerm : list) {
            //处理userperm的irCode字段
            String ircCode = (String) userPerm.get("irc_code");
            for (String disCode:disList) {
                if (ircCode == null || "".equals(ircCode)) {
                    ircCode = disCode;
                } else if(!ircCode.contains(disCode)){
                    ircCode = ircCode.concat(","+disCode);
                }
            }
            userPerm.put("irc_code", ircCode);
            //处理userperm的divCode字段
            String divCode = (String) userPerm.get("div_code");
            for (String dCode:divList) {
                if (divCode == null || "".equals(divCode)) {
                    divCode = dCode;
                } else  if(!divCode.contains(dCode)){
                    divCode = divCode.concat(","+dCode);
                }
            }
            userPerm.put("div_code", divCode);
            //处理userperm的vlsCode字段
            String vlsCode = (String) userPerm.get("vls_code");
            for (String vCode:divList) {
                if (vlsCode == null || "".equals(vlsCode)) {
                    vlsCode = vCode;
                } else if(!vlsCode.contains(vCode)){
                    vlsCode = vlsCode.concat(","+vCode);
                }
            }
            userPerm.put("vls_code", vlsCode);
            //如果三个都为空 则不运行更新语句
            if(disList.size()>0||divList.size()>0||volList.size()>0){
                pricingConfigAuthorMapper.updateUserPerm(userPerm);
            }
        }
        //更新tsys_dept_perm
        List<Map> deptList = pricingConfigAuthorMapper.selectDeptCfgRight(new HashMap());
        for (Map deptPerm : deptList) {
            //处理userperm的irCode字段
            String ircCode = (String) deptPerm.get("irc_code");
            for (String disCode:disList) {
                if (ircCode == null || "".equals(ircCode)) {
                    ircCode = disCode;
                } else if(!ircCode.contains(disCode)){
                    ircCode = ircCode.concat(","+disCode);
                }
            }
            deptPerm.put("irc_code", ircCode);
            //处理userperm的divCode字段
            String divCode = (String) deptPerm.get("div_code");
            for (String dCode:divList) {
                if (divCode == null || "".equals(divCode)) {
                    divCode = dCode;
                } else  if(!divCode.contains(dCode)){
                    divCode = divCode.concat(","+dCode);
                }
            }
            deptPerm.put("div_code", divCode);
            //处理userperm的vlsCode字段
            String vlsCode = (String) deptPerm.get("vls_code");
            for (String vCode:divList) {
                if (vlsCode == null || "".equals(vlsCode)) {
                    vlsCode = vCode;
                } else if(!vlsCode.contains(vCode)){
                    vlsCode = vlsCode.concat(","+vCode);
                }
            }
            deptPerm.put("vls_code", vlsCode);
            //如果三个都为空 则不运行更新语句
            if(disList.size()>0||divList.size()>0||volList.size()>0){
                pricingConfigAuthorMapper.updateDeptPerm(deptPerm);
            }
        }

    }
}
